home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / strategy / tictac2-.000 / tictac2- / tictac2 / ai.c next >
C/C++ Source or Header  |  1980-01-04  |  4KB  |  88 lines

  1. /*****************************************************************************.
  2. **  TicTac2:  Simple TicTacToe two-player game against another user or cpu.  **
  3. **  Copyright (c) 1995 Ian Singh                                             **
  4. **                                                                           **
  5. **  This program is free software; you can redistribute it and/or modify it  **
  6. **  under the terms  of the  GNU General Public License as published by the  **
  7. **  Free Software Foundation;  either version 2 of the License, or (at your  **
  8. **  option) any version later.                                               **
  9. **                                                                           **
  10. **  This program is distriubted in the hope that it  will  be  useful,  but  **
  11. **  WITHOUT   ANY   WARRANTY;   without   even   the  implied  warranty  of  **
  12. **  MERCHANTABILITY or FITNESS FOR  A  PARTICULAR  PURPOSE.   See  the  GNU  **
  13. **  General Public License for more details.                                 **
  14. **                                                                           **
  15. **  You should have received a copy of the GNU General Public License along  **
  16. **  with  this  program;   if not,  write  to the Free Software Foundation,  **
  17. **  Inc.,  675 Mass Ave, Cambridge, MA 02139, USA.                           **
  18. **                                                                           **
  19. **  Ian Singh                           Ian Singh                            **
  20. **  am256@freenet.carleton.ca           3G Arnold Dr.                        **
  21. **                                      Nepean, Ontario                      **
  22. **                                      K2H 6V6                              **
  23. .*****************************************************************************/
  24.  
  25. /* +-----------------------------------------------------------------------+ **
  26. ** |  ai.c:  ai routines for tictac2                                       | **
  27. ** |  Ian Singh. '95 (am256@freenet.carleton.ca)                           | **
  28. ** +-----------------------------------------------------------------------+ */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "tictac2.h"
  33. #include "ai.h"
  34. #include "moves.h"
  35.  
  36. int defense(char *field);
  37.  
  38. /* okay, here's the computer strategy.  first it sees if it can win by
  39.    one move, if it can it will.  If it can't it will look to see if the
  40.    opponent can win by one move only.  If it sees that it will block his
  41.    move.  If it can't do either of those it will then try to set itself
  42.    up for a win.  
  43. */
  44.  
  45. /* +------------------------------------------------------+ 
  46.    | edit this to make the computer change it's moves...  | 
  47.    | be careful.  Only put numbers between 0 and 8        |
  48.    +------------------------------------------------------+  */
  49.  
  50. int computer_moves[]={ 4,0,8,2,6,3,5,7,1 };
  51.  
  52. int wins[]={ 0,1,2, 1,2,0, 0,2,1, 3,4,5, 4,5,3, 3,5,4, 6,7,8, 7,8,6, 6,8,7,
  53. 0,3,6, 3,6,0, 0,6,3, 1,4,7, 4,7,1, 1,7,4, 2,5,8, 5,8,2, 2,8,5, 
  54. 0,4,8, 4,8,0, 0,8,4, 2,4,6, 4,6,2, 2,6,4 };
  55.   
  56. int getmove_cpu(player p, char *field)
  57. {
  58.   int i;
  59.  
  60.    /* offense: try for a one-move win */
  61.    for ( i=0; i<72; i+=3 )
  62.      if ( field[wins[i]]+field[wins[i+1]]==20 )
  63.         if ( !field[wins[i+2]] )  {
  64.           field[wins[i+2]]=p.num;
  65.           return 1;
  66.         }
  67.  
  68.   /* defense: block probable wins */
  69.   for ( i=0; i<72; i+=3 )
  70.     if ( field[wins[i]]+field[wins[i+1]]==2 )
  71.       if ( !field[wins[i+2]] )  {
  72.         field[wins[i+2]]=p.num;
  73.         return 1;
  74.       }
  75.  
  76.  
  77.  
  78.     /* offense: your average-every day move :-) */
  79.     for ( i=0; i<9; i++ )
  80.       if ( !field[computer_moves[i]] ) {
  81.         field[computer_moves[i]]=p.num;
  82.         return 1;
  83.       }
  84.  
  85.     return 0;        /* something terrible has happened. */
  86.        
  87. }
  88.